﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WS.EKA.FJYCM.Business.Model;
using WS.EKA.FJYCM.Utilities;

namespace WS.EKA.FJYCM.Business.DAL
{

    public class CoreBusinessSystemAccess
    {
        static IDB Pdb = DBFactory.CreatePDB();
        static IDB ARdb = DBFactory.CreateARDB();

        /// <summary>
        /// 获取核心业务系统
        /// </summary>
        /// <returns></returns>
        public static List<CoreBusinessSystem> GetBusinessSystem()
        {
            return ARdb.ExecuteDataTable("select distinct a.AppSystemName,a.instance_id from businessSystem@padmin b join AST_BMC_AppSystem a on b.name=a.appsystemname " +
            "where a.category = '应用系统' and a.securitylevel = '第三级' and siteid =101").ToList<CoreBusinessSystem>();
        }

        /// <summary>
        /// 根据IP和设备类型获取警报数量
        /// </summary>
        /// <param name="iP"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int GetAlertNumberByIPAndType(string iP, string type)
        {
            int number = 0;
            var sql = Pdb.ExecuteScalar(string.Format("select count(*) from device d join alert a on d.id =a.deviceid " +
            "where a.status='OPEN' and d.identify='{0}' and d.type='{1}' ", iP, type));
            if (sql.Any())
            {
                number = int.Parse(sql);
            }
            return number;
        }

        /// <summary>
        /// 根据IP和设备类型获取是否需要警报
        /// </summary>
        /// <param name="iP"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsAlertByIPAndType(string iP, string type)
        {
            bool isAlert = false;
            var sql="select count(*) from device d join alert a on d.id =a.deviceid where a.status='OPEN' ";
            if(!string.IsNullOrWhiteSpace(iP))
            {
                sql+=string.Format(" and d.identify='{0}'",iP);
            }
            if(!string.IsNullOrWhiteSpace(type))
            {
                sql+=string.Format(" and d.type='{0}'",type);
            }
            var count = Pdb.ExecuteScalar(sql);
            if (int.Parse(count)>0)
            {
                isAlert = true;
            }
            return isAlert;
        }

        /// <summary>
        /// 根据IP获取设备信息
        /// </summary>
        /// <param name="iP"></param>
        /// <returns></returns>
        public static List<CoreBusinessSystem> GetBusinessSystemByIP(string iP)
        {
            return Pdb.ExecuteDataTable(
                string.Format("select d.type,d.object objects,d.identify from device d join alert a on d.id =a.deviceid where a.status='OPEN' and d.identify='{0}'", iP))
                .ToList<CoreBusinessSystem>();
        }
    }
}
